//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
 * Copyright (C) 2002-2003 Red Hat, Inc.
 *
 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
 * $Id: os-elaos.h,v 1.5 2006/10/16 03:06:19 yinbo Exp $
 *
 */

#ifndef __JFFS2_OS_ELAOS_H__
#define __JFFS2_OS_ELAOS_H__

#include "ddk.h"
#include <stdlib.h>
#include <string.h>

#include <elatypes.h>

#include "jffs2_fs_i.h"
#include "jffs2_fs_sb.h"
#include "jffs2_limit.h"
#include "flashctrl.h"
#include "compr.h"
#include "jffs2_iovec.h"
#include "jffs2.h"
#include "crc32.h"
#include "jffs2_semaphore.h"
#include "jffs2_spin_lock.h"
#include "jffs2_fileFlag.h"

#ifndef JFFS2_SSIZE_T
#define JFFS2_SSIZE_T
typedef unsigned long ssize_t;
#endif

#define PTR_ERR(err) ((unsigned long)(err))
#define IS_ERR(err) ((unsigned long)err > (unsigned long)-1000L)

//======================================================================
// dirent structure.

struct dirent
{
    char        d_name[NAME_MAX_LEN+1];
};

#define ERR_PTR(err) ((void*)(err))

#define printf kprintf
#define printfk kprintf

extern "C" inline time_t get_seconds()
{
    SystemTime t;
    DzGetSystemTime(&t);
    return t.seconds;
}

struct _inode;
struct super_block;

static inline unsigned int full_name_hash(const unsigned char * name,
                unsigned int len)
{

    unsigned hash = 0;
    while (len--) {
        hash = (hash << 4) | (hash >> 28);
        hash ^= *(name++);
    }
    return hash;
}

#ifdef ELAOPT_FS_JFFS2_WRITE
#define jffs2_is_readonly(c) (0)
#else
#define jffs2_is_readonly(c) (1)
#endif

/* NAND flash not currently supported on ezos */
#define jffs2_can_mark_obsolete(c) (1)

#define JFFS2_INODE_INFO(i) (&(i)->jffs2_i)
#define OFNI_EDONI_2SFFJ(f)  \
    ((struct _inode *) ( ((char *)f) - (((char *)\
    (&((struct _inode *)1)->jffs2_i)) - 1) ) )

#define JFFS2_F_I_SIZE(f) (OFNI_EDONI_2SFFJ(f)->i_size)
#define JFFS2_F_I_MODE(f) (OFNI_EDONI_2SFFJ(f)->i_mode)
#define JFFS2_F_I_UID(f) (OFNI_EDONI_2SFFJ(f)->i_uid)
#define JFFS2_F_I_GID(f) (OFNI_EDONI_2SFFJ(f)->i_gid)
#define JFFS2_F_I_CTIME(f) (OFNI_EDONI_2SFFJ(f)->i_ctime)
#define JFFS2_F_I_MTIME(f) (OFNI_EDONI_2SFFJ(f)->i_mtime)
#define JFFS2_F_I_ATIME(f) (OFNI_EDONI_2SFFJ(f)->i_atime)

/* FIXME: eCos doesn't hav a concept of device major/minor numbers */
#define JFFS2_F_I_RDEV_MIN(f) ((OFNI_EDONI_2SFFJ(f)->i_rdev)&0xff)
#define JFFS2_F_I_RDEV_MAJ(f) ((OFNI_EDONI_2SFFJ(f)->i_rdev)>>8)

typedef short dev_t;
typedef unsigned int ino_t;
typedef unsigned int mode_t;
typedef unsigned short nlink_t;
#define off_t uint32_t
#define loff_t off_t
typedef unsigned short gid_t;
typedef unsigned short uid_t;
typedef uint32_t cyg_uint32;

struct _inode
{
    uint32_t  i_ino;

    int   i_count;
    mode_t   i_mode;
    nlink_t   i_nlink; // Could we dispense with this?
    uid_t   i_uid;
    gid_t   i_gid;
    time_t   i_atime;
    time_t   i_mtime;
    time_t   i_ctime;
    // union {
    unsigned short i_rdev; // For devices only
    struct _inode * i_parent; // For directories only
    off_t  i_size; // For files only
    // };
    struct super_block * i_sb;

    struct jffs2_inode_info jffs2_i;

    struct _inode *  i_cache_prev; // We need doubly-linked?
    struct _inode *  i_cache_next;
};

#define JFFS2_SB_INFO(sb) (&(sb)->jffs2_sb)
#define OFNI_BS_2SFFJ(c)  \
    ((struct super_block *) \
    ( ((char *)c) - (((char *)(&((struct super_block *)1)->jffs2_sb)) - 1) ) )

struct super_block
{
    struct jffs2_sb_info jffs2_sb;
    struct _inode *  s_root;
    IDeviceDriver *s_dev;
    uint32_t  s_mount_count;

#if 0

    cyg_io_handle_t  s_dev;

#ifdef ELAOPT_FS_JFFS2_GCTHREAD

    cyg_mutex_t s_lock;             // Lock the inode cache
    cyg_flag_t  s_gc_thread_flags;  // Communication with the gcthread
    cyg_handle_t s_gc_thread_handle;
    cyg_thread s_gc_thread;
#if (CYGNUM_JFFS2_GC_THREAD_STACK_SIZE >= CYGNUM_HAL_STACK_SIZE_MINIMUM)

    char s_gc_thread_stack[CYGNUM_JFFS2_GC_THREAD_STACK_SIZE];
#else

    char s_gc_thread_stack[CYGNUM_HAL_STACK_SIZE_MINIMUM];
#endif

    cyg_mtab_entry *mte;
#endif
#endif
};

struct ELA_FILE_TAG
{
    uint32_t               f_flag;  /* file state                   */
    uint16_t                  f_ucount;       /* use count                    */
    uint16_t                  f_type;  /* descriptor type              */
    uint32_t                  f_syncmode;     /* synchronization protocol     */
    uint32_t                  f_offset;       /* current offset               */
    struct _inode*           f_data;  /* file or socket               */
    //class FileSystem*         f_fileSystem;         /* mount table entry            */
};

typedef struct ELA_FILE_TAG n_file;

class FileSystem
{
public:
    int jffs2_mount(wchar_t *wcszDeviceName);
    int jffs2_umount();

    struct _inode* jffs2_get_root() const
        {
            return pSuperBlock->s_root;
        }
    struct super_block * jffs2_get_superBlock() const
        {
            return pSuperBlock;
        }
#if 0
    int jffs2_getinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
                      int key, void *buf, int len);
    int jffs2_setinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
                      int key, void *buf, int len);
#endif

private:
    struct super_block *pSuperBlock;
    //struct _inode *pRoot;
    static unsigned char n_fs_mounted;  // a counter to track the number of jffs2 instances mounted
};

class IoOperator
{
public:
    // File operations
    static int jffs2_fo_read(struct ELA_FILE_TAG *fp, struct ELA_UIO_TAG *uio);
#ifdef ELAOPT_FS_JFFS2_WRITE

    static int jffs2_fo_write(struct ELA_FILE_TAG *fp, struct ELA_UIO_TAG *uio);
    static int jffs2_fo_extend(struct ELA_FILE_TAG *fp, off_t len);
#endif

    static int jffs2_fo_lseek(struct ELA_FILE_TAG *fp, off_t * pos, int whence);
    /*static int jffs2_fo_ioctl(struct ELA_FILE_TAG *fp, ELA_ADDRWORD com,
                  ELA_ADDRWORD data);
    static int jffs2_fo_fsync(struct ELA_FILE_TAG *fp, int mode);*/
    static int jffs2_fo_close(struct ELA_FILE_TAG *fp);
    static int jffs2_fo_fstat(struct ELA_FILE_TAG *fp, struct stat *buf);
    static int jffs2_fo_set_fstat(struct ELA_FILE_TAG *fp, struct stat *buf);

    /*static int jffs2_fo_getinfo(struct ELA_FILE_TAG *fp, int key, void *buf,
                    int len);
    static int jffs2_fo_setinfo(struct ELA_FILE_TAG *fp, int key, void *buf,
                    int len);*/

    // Directory operations
    static int jffs2_fo_dirread(struct ELA_FILE_TAG *fp, struct ELA_UIO_TAG *uio);
    static int jffs2_fo_dirlseek(struct ELA_FILE_TAG *fp, off_t * pos, int whence);

    static int jffs2_truncate_file (struct _inode *inode, uint32_t size);
    static int jffs2_open(struct _inode* dir, char *name,
                          int mode, n_file * fte);
#ifdef ELAOPT_FS_JFFS2_WRITE

    static int jffs2_ops_unlink(struct _inode* dir,
                                char *name);
    static int jffs2_ops_mkdir(struct _inode* dir, char *name, int mode);
    static int jffs2_ops_rmdir(struct _inode* dir, char *name);
    static int jffs2_ops_rename(struct _inode* dir1,
                                char *name1, struct _inode* dir2, char *name2);
    static int jffs2_ops_link(struct _inode* dir1, char *name1,
                              struct _inode* dir2, char *name2, int type);
#endif

    static int jffs2_opendir(struct _inode* dir, char *name,
                    n_file * fte);
    static int jffs2_chdir(struct _inode* dir, char *name,
                    struct _inode** dir_out);
    static int jffs2_stat(struct _inode* dir, char *name,
                    struct stat *buf);
};

#define sleep_on_spinunlock(wq, sl) spin_unlock(sl)
#define EBADFD 32767

/* background.c */
#ifdef ELAOPT_FS_JFFS2_GCTHREAD
void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
void jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
#else
static inline void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
{
    /* We don't have a GC thread in eCos (yet) */
}
#endif

/* fs-ecos.c */
struct _inode *jffs2_new_inode (struct _inode *dir_i, int mode,
                struct jffs2_raw_inode *ri);
struct _inode *jffs2_iget(struct super_block *sb, cyg_uint32 ino);
void jffs2_iput(struct _inode * i);
void jffs2_gc_release_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f);
struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c, int inum,
                int nlink);
unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
                struct jffs2_inode_info *f,
                unsigned long offset, unsigned long *priv);
void jffs2_gc_release_page(struct jffs2_sb_info *c, unsigned char *pg, 
                unsigned long *priv);

/* Avoid polluting eCos namespace with names not starting in jffs2_ */
#define os_to_jffs2_mode(x) jffs2_from_os_mode(x)
uint32_t jffs2_from_os_mode(uint32_t osmode);
uint32_t jffs2_to_os_mode (uint32_t jmode);

/* flashio.c */
int jffs2_flash_read(struct jffs2_sb_info *c, cyg_uint32 read_buffer_offset,
                const size_t size, size_t * return_size,
                unsigned char * write_buffer);
int jffs2_flash_write(struct jffs2_sb_info *c, cyg_uint32 write_buffer_offset,
                const size_t size, size_t * return_size,
                unsigned char * read_buffer);
int jffs2_flash_direct_writev(struct jffs2_sb_info *c,
                const struct ELA_IOVEC_TAG *vecs,
                unsigned long count, loff_t to, size_t *retlen);
int jffs2_flash_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);

// dir-ecos.c
struct _inode *jffs2_lookup(struct _inode *dir_i, const unsigned char *name,
                int namelen);
int jffs2_create(struct _inode *dir_i, const unsigned char *d_name, int mode,
                struct _inode **new_i);
int jffs2_mkdir (struct _inode *dir_i, const unsigned char *d_name, int mode);
int jffs2_link (struct _inode *old_d_inode, struct _inode *dir_i,
                const unsigned char *d_name);
int jffs2_unlink(struct _inode *dir_i, struct _inode *d_inode,
                const unsigned char *d_name);
int jffs2_rmdir (struct _inode *dir_i, struct _inode *d_inode,
                const unsigned char *d_name);
int jffs2_rename (struct _inode *old_dir_i, struct _inode *d_inode,
                const unsigned char *old_d_name, struct _inode *new_dir_i,
                const unsigned char *new_d_name);

/* erase.c */
static inline void jffs2_erase_pending_trigger(struct jffs2_sb_info *c)
{
}

#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
#define SECTOR_ADDR(x) ( ((unsigned long)(x) & ~(c->sector_size-1)) )
#define jffs2_can_mark_obsolete(c) (1)
#define jffs2_cleanmarker_oob(c) (0)
#define jffs2_write_nand_cleanmarker(c,jeb) (EIO)

#define jffs2_flush_wbuf_pad(c) (c=c)
#define jffs2_flush_wbuf_gc(c, i) ({ (void)(c), (void) i, 0; })
#define jffs2_nand_read_failcnt(c,jeb) do { ; } while(0)
#define jffs2_write_nand_badblock(c,jeb,p) (0)
#define jffs2_flash_setup(c) (0)
#define jffs2_nand_flash_cleanup(c) do {} while(0)
#define jffs2_wbuf_dirty(c) (0)
#define jffs2_flash_writev(a,b,c,d,e,f) jffs2_flash_direct_writev(a,b,c,d,e)
#define jffs2_wbuf_timeout NULL
#define jffs2_wbuf_process NULL
#define jffs2_nor_ecc(c) (0)
#else
#error no nand yet
#endif

#define BUG() assert(0)

#ifndef BUG_ON
#define BUG_ON(x) do { if ((x)) BUG(); } while(0)
#endif

#define __init

#define min(a, b) ((a) < (b))?(a):(b)
#define max(a, b) ((a) > (b))?(a):(b)
#define min_t(t, x,y) ((t)x<(t)y?(t)x:(t)y)

#define likely(x) (x)
#define unlikely(x) (x)

#endif /* __JFFS2_OS_ELAOS_H__ */
